package com.linyaonan.leetcode.easy._606;

/**
 * @author: Lin
 * @date: 2020/1/4
 */
public class ConstructString {
    StringBuilder sb = new StringBuilder();

    public String tree2str(TreeNode t) {
        if (t == null) {
            return "";
        }
        bfs(t);
        return sb.toString();
    }

    private void bfs(TreeNode node) {
        if (node != null) {
            sb.append(node.val);
            // 左或者右节点不为空是，一样需要输出
            if (node.left != null || node.right != null) {
                sb.append("(");
                bfs(node.left);
                sb.append(")");
            }

            if (node.right != null) {
                sb.append("(");
                bfs(node.right);
                sb.append(")");
            }
        }
    }
}
